<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2015-2016 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionAppApi\Controller;

use Think\Controller;
use AuctionCore\System\AuctionException;
use AuctionCore\CarResource\CarReport;

/**
 * 检测报告
 *
 * @author Xmh
 */
class ReportController extends BaseController {

    private $car_id = null;
    public $h5_actions = array('C_REPORT|A_REPAIR_HISTORY','C_REPORT|A_DM_POINT_CLASS');

    public function __construct() {
        parent::__construct();
        $this->car_id = I("param.car_id/d");
        if (empty($this->car_id)) {
            AuctionException::make_throw('600001');
        }
        $this->report = new CarReport($this->car_id);
    }

    /**
     * 检测报告 - 车辆、车况信息
     * return json  车辆、车况信息
     */
    public function index() {
        $report_title[] = $this->report->header();
        if (array_filter($report_title)) {
            $report_title_info = array();
            foreach ($report_title as $v) {
                $pointer_mileage = $v['pointer_mileage'];
                $first_reg_date = date("Y年m月", strtotime($v['first_reg_date'])) . '初登';
                $report_title_info['car_name'] = $v['app_model_show'];
//                $report_title_info['first_reg_date'] = $first_reg_date;
//                $report_title_info['pointer_mileage'] = $pointer_mileage;
//                $report_title_info['gearbox'] = C('GEARBOX')[$v['gearbox']];
//                $report_title_info['plate_number'] = $v['plate_number'];
                $report_title_info['summery'] = $first_reg_date . '/' . $pointer_mileage . '/' . C('GEARBOX')[$v['gearbox']] . '/' . $v['plate_number'];
                ;
                $report_title_info['car_no'] = $v['car_no'];
                $report_title_info['check_date'] = date("Y年m月d日", strtotime($v['check_date']));
                $report_title_info['checker'] = $v['checker'];
                $report_title_info['checker_level'] = $v['checker_level'];
//                $report_title_info['composite_state'] = $v['composite_state'];
//                $report_title_info['acc_level'] = $v['acc_level'];
                $report_title_info['composite_level'] = $v['composite_state'] . $v['acc_level'];
//                $report_title_info['composite_state_desc'] = $v['composite_state_desc'];
//                $report_title_info['acc_level_desc'] = $v['acc_level_desc'];
                $report_title_info['desc'] = $v['composite_state_desc'] . $v['acc_level_desc'];
            }
            $this->api_200($report_title_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 检测报告 - 基本信息
     * return array  车辆信息
     */
    public function base_info() {
        $base = $this->report->app_base_info();
        if ($base) {
            $base['first_reg_date']['value'] = date("Y年m月d日", strtotime($base['first_reg_date']['value'])) . '初登';
            $base['ex_factory_date']['value'] = date("Y年m月d日", strtotime($base['ex_factory_date']['value']));
            $base_info = $this->getObjArr($base);
            $this->api_200($base_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 转换报告信息为对象格式
     * @param type $arr
     * @return type
     */
    private function getObjArr($arr) {
        $i = 0;
        $arr_info = array();
        foreach ($arr as $va) {
            $arr_info[$i] = $va;
            $i++;
        }
        return $arr_info;
    }

    /**
     * 检测报告 - 手续信息
     * return json 手续信息
     */
    public function procedure() {
        $procedure_info[] = $this->report->procedure();
        if (array_filter($procedure_info)) {
            foreach ($procedure_info as $pi) {
                foreach ($pi as $k => $p) {
                    if ($p['value'] == '无' || $p['value'] == '未见') {
                        $pi[$k]['style'] = 1;
                    } else {
                        $pi[$k]['style'] = 0;
                    }
                }
            }
            $pi_info = $this->getObjArr($pi);
            $this->api_200($pi_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 检测报告 - 配置信息
     * return json 配置信息
     */
    public function config() {
        $config_info[] = $this->report->configuration();
        if (array_filter($config_info)) {
            foreach ($config_info as $ci) {
                foreach ($ci as $k => $c) {
                    if ($c['value'] == '无') {
                        $ci[$k]['style'] = 1;
                    } else {
                        $ci[$k]['style'] = 0;
                    }
                }
            }
            $ci_info = $this->getObjArr($ci);
            $this->api_200($ci_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 检测报告 - 工况及附件说明
     * return json 配置信息
     */
    public function perform() {
//        $perform_info = $this->report->perform();
        $perform_info = $this->report->get_app_perform($this->car_id);
        if ($perform_info) {
            $perform_info = $this->getObjArr($perform_info);
            $this->api_200($perform_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 检测报告 - 基本照片
     * return json 车辆基本照片
     */
    public function base_pics() {
        $base_pics_info = $this->report->get_base_pics_app();
        if ($base_pics_info) {
            $this->api_200($base_pics_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 检测报告 - 修复历史页 - 外观
     * return json 外观修复历史
     */
    public function appear_history() {
        $appear_history_info = $this->report->appear_history();
        if ($appear_history_info) {
            $this->api_200($appear_history_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 检测报告 - 修复历史页 - 骨架
     * return json 骨架修复历史
     */
    public function frame_history() {
        $frame_history_info = $this->report->frame_history();
        if ($frame_history_info) {
            $this->api_200($frame_history_info);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 检测报告 - 可见损伤部位-外观、内饰、骨架
     * return json 可见损伤部位
     */
    public function get_dm_position() {
//        $type = I('post.type/d');
        $dm_position['ns'] = $this->report->get_dm_type('1');
        $dm_position['wg'] = $this->report->get_dm_type('2');
        $dm_position['frame'] = $this->report->get_dm_type('3');
        if (array_filter($dm_position)) {
            $this->api_200($dm_position);
        } else {
            AuctionException::make_throw('600002');
        }
    }

    /**
     * 获取可见损伤点样式
     * @param string $type (inside,outside,frame)
     */
    public function get_dm_point_class() {
        $dm_point_class = $this->report->get_dm_point_class();
        $this->api_200($dm_point_class);
    }

    /**
     * 11个覆盖件与7个骨架位置
     *  Car/report_history.html
     *  <div class="xiufuBg{$pos}" style="display:block"></div>
     *  例：左前翼子板 重新喷漆 <div class="xiufuBg1" style="display:block"></div>
     *  例：左前翼子板 重新喷漆且有腻子 <div class="xiufuBg_1" style="display:block"></div>
     * @param string $type 类型（appear 外观 ,frame 骨架
     *  @return Array
     */
    public function get_pos_class() {
        $pos_class['appear11'] = $this->report->get_pos_class();
        $pos_class['frame7'] = $this->report->get_pos_class('frame');
        if (array_filter($pos_class)) {
            $this->api_200($pos_class);
        } else {
            AuctionException::make_throw('600008');
        }
    }

    /**
     * 检测报告 - 可见损伤部位照片
     * return json 可见损伤部位照片
     */
    public function get_dm_position_pics() {
        $dm_id = I("post.dm_id/d");
        $dm_pics = $this->report->get_dm_position_pics($dm_id);
        if ($dm_pics) {
            $this->api_200($dm_pics);
        } else {
            AuctionException::make_throw('600007');
        }
    }

    /**
     * 检测报告 - 可见损伤程度照片
     * return json 可见损伤程度照片
     */
    public function get_dm_degree_pics() {
        $dm_degree_pics = $this->report->get_dm_degree_pics();
        $this->api_200($dm_degree_pics);
    }

    /**
     * 外观与骨架修复历史H5
     */
    public function repair_history() {
        $report = array();
        //外观修复历史
        $report['appear_history'] = $this->report->appear_history();
        //骨架修复历史
        $report['frame_history'] = $this->report->frame_history();
        //外观色块
        $report['appear11'] = $this->report->get_pos_class();
        //骨架色块
        $report['frame7'] = $this->report->get_pos_class('frame');
        $this->assign('report', $report);
        $this->display('Report/repair_history');
    }

    /**
     * 可见损伤点图
     */
    public function dm_point_class() {
        $report = array();
        $arr = $this->report->get_dm_point_class();
        $frame = $arr['frame']; //24处损伤位置，不包括自定义位置
        foreach ($frame as $v) {
            if ($v) {
                $report['frame24'][] = $v;
            }
        }
        unset($v);
        $outside = $arr['outside']; //41处损伤位置，不包括自定义位置
        foreach ($outside as $v) {
            if ($v) {
                $report['outside41'][] = $v;
            }
        }
        unset($v);
        $inside = $arr['inside']; //29处操作位置，不包括自定义位置
        foreach ($inside as $v) {
            if ($v) {
                $report['inside29'][] = $v;
            }
        }
        $arr = $this->report->get_dm_describe();
        $report['desc_frame'] = $arr['frame'];
        $report['desc_outside'] = $arr['outside'];
        $report['desc_inside'] = $arr['inside'];

        $this->assign('report', $report);
        $this->display('Report/visiable_damage');
    }
    //-----------------------------------------车源报告(纯接口)-----------------------------------------
    //-----------------------------------------车源报告(纯接口)-----------------------------------------
    public function report(){

        if(empty($this->report->report['car_base'])){
            AuctionException::make_throw('600002');
        }

        $report = array();
        $title = $this->report_title();
        $report['title'] = $title;
        $report['base_info'] = $this->report_base_info();
        $report['procedure'] = $this->report_procedure();
        $report['config'] = $this->report_config();
        $report['perform'] = $this->report_perform();
        $report['base_pics'] = $this->report->get_base_pics_app();

        //隐性损伤文字描述
        $report['hidden']['frame'] = $this->report->frame_history_v100();
        $report['hidden']['appear'] = $this->report->appear_history_v100();
        //隐性损伤图片
        $report['hidden']['frame']['pic'] = $title['damage_pics']['hidden']['frame'];
        $report['hidden']['appear']['pic'] = $title['damage_pics']['hidden']['outside'];

        //显性损伤文字描述
        $desc = $this->report->get_dm_describe();
        $report['visible']['frame'] = $desc['frame']?$desc['frame']:array();
        $report['visible']['outside'] = $desc['outside']?$desc['outside']:array();
        $report['visible']['inside'] = $desc['inside']?$desc['inside']:array();
        //显性损伤图片
        $report['visible']['frame']['pic'] = $title['damage_pics']['visible']['frame'];
        $report['visible']['outside']['pic'] = $title['damage_pics']['visible']['outside'];
        $report['visible']['inside']['pic'] = $title['damage_pics']['visible']['inside'];

        //轻微，一般，严重
        $degree = $this->get_dm_by_degree();

        $report['degree']['light'] = $degree['light']?$degree['light']:array();
        $report['degree']['normal'] = $degree['normal']?$degree['normal']:array();
        $report['degree']['serious'] = $degree['serious']?$degree['serious']:array();

        $this->api_200($report);

    }
        //-------------检测报告支持函数
        /**
         * 检测报告 - 车辆、车况信息
         * return json
         */
        private  function report_title() {
            $report_title[] = $this->report->header();
            if (array_filter($report_title)) {
                foreach ($report_title as $v) {
                    $report_title_info = $v;
                    $pointer_mileage = convert_mileage($v['pointer_mileage']) . '万公里';
                    $first_reg_date = date("Y年m月", strtotime($v['first_reg_date'])) . '初登';
                    $report_title_info['car_name'] = $v['app_model_show'];
                    $report_title_info['summery'] = $first_reg_date . '/' . $pointer_mileage . '/' . C('GEARBOX')[$v['gearbox']] . '/' . $v['plate_number'];
                    $report_title_info['car_no'] = $v['car_no'];
                    $report_title_info['check_date'] = date("Y年m月d日", strtotime($v['check_date']));
                    $report_title_info['checker'] = $v['checker'];
                    $report_title_info['checker_level'] = $v['checker_level'];
                    $report_title_info['composite_level'] = $v['composite_state'] . $v['acc_level'];
                    $report_title_info['desc'] = $v['composite_state_desc'] . $v['acc_level_desc'];
                    $report_title_info['desc_app'] = $v['composite_state_desc'] . $v['acc_level_desc'] . $v['history_state'];
                }
            }
            return $report_title_info;
        }

        /**
         * 检测报告 - 基本信息
         * return array
         */
    private function report_base_info() {
            $base = $this->report->app_base_info();
            if ($base) {
                $base['first_reg_date']['value'] = date("Y年m月d日", strtotime($base['first_reg_date']['value'])) . '初登';
                $base['ex_factory_date']['value'] = date("Y年m月d日", strtotime($base['ex_factory_date']['value']));
                $base_info = $this->getObjArr($base);
            }
            return $base_info;
        }


        /**
         * 检测报告 - 手续信息
         * return json
         */
        private function report_procedure() {
            $procedure_info[] = $this->report->procedure();
            if (array_filter($procedure_info)) {
                foreach ($procedure_info as $pi) {
                    foreach ($pi as $k => $p) {
                        if ($p['value'] == '无' || $p['value'] == '未见') {
                            $pi[$k]['style'] = 1;
                        } else {
                            $pi[$k]['style'] = 0;
                        }
                    }
                }
                $pi_info = $this->getObjArr($pi);
            }
            return $pi_info;
        }

        /**
         * 检测报告 - 配置信息
         * return json
         */
        private function report_config() {
            $config_info[] = $this->report->configuration();
            if (array_filter($config_info)) {
                foreach ($config_info as $ci) {
                    foreach ($ci as $k => $c) {
                        if ($c['value'] == '无') {
                            $ci[$k]['style'] = 1;
                        } else {
                            $ci[$k]['style'] = 0;
                        }
                    }
                }
                $ci_info = $this->getObjArr($ci);
            }
            return $ci_info;
        }

        /**
         * 检测报告 - 工况及附件说明
         * return json
         */
        private function report_perform() {
            $perform_info = $this->report->get_app_perform($this->car_id);
            if ($perform_info) {
                $perform_info = $this->getObjArr($perform_info);
            }
            return $perform_info;
        }
    private function  get_dm_by_degree(){
        $arr = array();
        foreach ($this->report->report['damage'] as $k => $value) {
            foreach($value['urls'] as $v){
            //$v['urls'] = array_pop($v['urls']);
                switch ($v['dm_degree']) {
                    case '2':
                        $arr['light'][$k] = $value;
                        $arr['light'][$k]['position_cn'] = $value['cus_pos_name']?$value['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.' . $value['position']);
                        break;
                    case '3':
                        $arr['normal'][$k] = $value;
                        $arr['normal'][$k]['position_cn'] = $value['cus_pos_name']?$value['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.' . $value['position']);
                        break;
                    case '4':
                        $arr['serious'][$k] = $value;
                        $arr['serious'][$k]['position_cn'] = $value['cus_pos_name']?$value['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.' . $value['position']);
                        break;
                }
            }
        }
        return $arr;
    }
}
